package com.wanlian.service.impl;
import com.wanlian.common.PageCondition;
import com.wanlian.dao.BlackListDao;
import com.wanlian.dao.DeviceDao;
import com.wanlian.entity.BlackList;
import com.wanlian.entity.Device;
import com.wanlian.model.ResultModel;
import com.wanlian.service.BlackListServices;
import com.wanlian.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BlackListServicesImpl implements BlackListServices {


    @Autowired
    public BlackListDao blackListDao;

    @Autowired
    public DeviceDao deviceDao;

    @Override
    public ResultModel doAdd(BlackList blackList,int userId) {
        ResultModel resultModel=new ResultModel();
        BlackList dbblackList=blackListDao.queryByMacId(blackList.getDeviceId(),blackList.getMacId());
        if(dbblackList!=null){
            resultModel.setBresult(false);
            resultModel.setMessage("该设备已存在黑名单");
        }else{
            Device device=deviceDao.queryCurrentByUserId(userId);
            blackList.setDeviceId(device.getId()+"");
            blackList.setConnetTime(DateUtil.currentTimestamp());
            blackListDao.add(blackList);
            resultModel.setBresult(true);
            resultModel.setMessage("添加成功");
        }
        return resultModel;
    }

    @Override
    public ResultModel doedit(BlackList blackList,int userId) {
        ResultModel resultModel=new ResultModel();
        blackListDao.delete(blackList.getId());
        resultModel.setBresult(true);
        resultModel.setMessage("删除成功");
        return resultModel;
    }

    @Override
    public ResultModel queryBlackList(PageCondition pageCondition,int userId) {
        Device device=deviceDao.queryCurrentByUserId(userId);
        int deviceId=device.getId();
        return blackListDao.queryBlackList(pageCondition,deviceId);
    }
}
